package com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget

import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import androidx.viewpager2.widget.ViewPager2
import com.hhsjdesign.dynamicui.annoprocess.AWidgetType
import com.hhsjdesign.dynamicui.core.widgetimpl.BusinessWidget
import com.hhsjdesign.dynamicui.interfaces.RequestListener
import com.hhsjdesign.dynamicui.businessdata.BaseModel
import com.hhsjdesign.dynamicui.businessdata.IViewable
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetData
import com.hhsjdesign.dynamicui.core.property.WidgetSpecProperty
import com.hhsjdesign.dynamicui.core.property.widget.BannerGWidgetProperty
import com.hhsjdesign.dynamicui.core.property.widget.BannerIndicatorProperty
import com.hhsjdesign.dynamicui.core.widgetdata.WidgetTypeConst
import com.hhsjdesign.dynamicui.core.widgetimpl.businesswidget.adapter.RecyclerSimpleAdapter

/**
 * <BannerSimpleProperty>
 */
@AWidgetType(WidgetTypeConst.BANNER_SINGLE)
class BannerSWidget : BusinessWidget {

    private var adapter: RecyclerSimpleAdapter? = null
    private var viewPager2: ViewPager2? = null
    private var indicator: FrameLayout? = null
    private var businessItemWidgetList: MutableList<IViewable<*>>? = null

    constructor(
        context: Context,
        widgetData: WidgetData
    ) : super(context, widgetData) {
        businessItemWidgetList = widgetData.businessData
    }

    override fun makeWidget() {
        super.makeWidget()
    }

    override fun createMainView(specProperty: WidgetSpecProperty): View {
        return FrameLayout(context).apply {
            viewPager2 = ViewPager2(context).apply {
                this.layoutParams = FrameLayout.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
            }
            this.addView(viewPager2)
            indicator = FrameLayout(context)
            this.addView(indicator)
            (specProperty as? BannerGWidgetProperty)?.indicatorProperty?.let {
                initIndicator(it)
            }
        }
    }

    //创建indicator
    private fun initIndicator(indicatorProperty: BannerIndicatorProperty) {

    }

    override fun loadLocalBusiness() {
        if (adapter == null) {
            adapter = RecyclerSimpleAdapter(
                context,
                widgetData.business!!,
                widgetData.itemPropertyList!![0].itemWidgetType,
                widgetData.itemPropertyList!![0].itemPropertyModel,
                businessItemWidgetList!!,
                -1,
                -1,
                widgetData.viewProperty!!.size[2]
            )
        }
        setAdapter()
    }

    override fun loadRemoteBusiness() {
        dataCaller?.loadData(
            widgetData.business!!,
            widgetData.params!!,
            this,
            object : RequestListener<Any> {
                override fun onRequestSuccess(
                    requestId: String,
                    result: BaseModel<Any>
                ) {
                    if (result.type == 1) {
                        if (result.resultList != null) {
                            if (adapter == null) {
                                adapter = RecyclerSimpleAdapter(
                                    context,
                                    widgetData.business!!,
                                    widgetData.itemPropertyList!![0].itemWidgetType,
                                    widgetData.itemPropertyList!![0].itemPropertyModel,
                                    result.toIViewableList() as MutableList<IViewable<*>>,
                                    -1,
                                    -1,
                                    widgetData.viewProperty!!.size[2]
                                )
                            }
                            setAdapter()
                        }
                    }
                }

                override fun onRequestFail(
                    requestId: String,
                    resultCode: Int,
                    resultMessage: String
                ) {
                }

            })
    }

    private fun setAdapter() {
        if (adapter != null && viewPager2 != null) {
            viewPager2!!.adapter = adapter
        }
    }

}